home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / Draw / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  15.6 KB  |  550 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Content.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "ODFDraw.hpp"
  11.  
  12. #ifndef CONTENT_H
  13. #include "Content.h"
  14. #endif
  15.  
  16. #ifndef DRAWPART_H
  17. #include "DrawPart.h"
  18. #endif
  19.  
  20. #ifndef DRAWCLIP_H
  21. #include "DrawClip.h"
  22. #endif
  23.  
  24. #ifndef BASESHP_H
  25. #include "BaseShp.h"
  26. #endif
  27.  
  28. #ifndef DRAWPRXY_H
  29. #include "DrawPrxy.h"
  30. #endif
  31.  
  32. #ifndef UTILS_H
  33. #include "Utils.h"
  34. #endif
  35.  
  36. #ifndef DRAWLINK_H
  37. #include "DrawLink.h"
  38. #endif
  39.  
  40. #ifndef DRWPRMSE_H
  41. #include "DrwPrmse.h"
  42. #endif
  43.  
  44. #ifndef DRAWSEL_H
  45. #include "DrawSel.h"
  46. #endif
  47.  
  48. // ----- Part Layer -----
  49.  
  50. #ifndef FWUTIL_H
  51. #include "FWUtil.h"
  52. #endif
  53.  
  54. #ifndef FWPRESEN_H
  55. #include "FWPresen.h"
  56. #endif
  57.  
  58. // ----- OS Layer -----
  59.  
  60. #ifndef FWRECT_H
  61. #include "FWRect.h"
  62. #endif
  63.  
  64. #ifndef FWODGEOM_H
  65. #include "FWODGeom.h"
  66. #endif
  67.  
  68. #ifndef FWSUSINK_H
  69. #include "FWSUSink.h"
  70. #endif
  71.  
  72. #ifndef FWBARRAY_H
  73. #include "FWBArray.h"
  74. #endif
  75.  
  76. #ifndef FWFILEAC_H
  77. #include "FWFileAc.h"
  78. #endif
  79.  
  80. #ifndef SLMixOS_H
  81. #include "SLMixOS.h"
  82. #endif
  83.  
  84. // ----- Foundation Layer -----
  85.  
  86. #ifndef FWSTREAM_H
  87. #include "FWStream.h"
  88. #endif
  89.  
  90. #ifndef FWSUSINK_H
  91. #include "FWSUSink.h"
  92. #endif
  93.  
  94. #ifndef FWMEMORY_H
  95. #include "FWMemory.h"
  96. #endif
  97.  
  98. // ----- OpenDoc Includes -----
  99.  
  100. #ifndef SOM_Module_OpenDoc_StdProps_defined
  101. #include <StdProps.xh>
  102. #endif
  103.  
  104. #ifndef SOM_ODTranslation_xh
  105. #include <Translt.xh>
  106. #endif
  107.  
  108. #ifndef SOM_ODShape_xh
  109. #include <Shape.xh>
  110. #endif
  111.  
  112. #ifndef SOM_ODStorageUnit_xh
  113. #include <StorageU.xh>
  114. #endif
  115.  
  116. #ifndef SOM_ODSession_xh
  117. #include <ODSessn.xh>
  118. #endif
  119.  
  120. //========================================================================================
  121. //    Runtime Information
  122. //========================================================================================
  123.  
  124. #ifdef FW_BUILD_MAC
  125. #pragma segment odfdraw2
  126. #endif
  127.  
  128. FW_DEFINE_AUTO(CDrawContent)
  129. FW_DEFINE_AUTO(CDrawContentShapeIterator)
  130. FW_DEFINE_AUTO(CSemanticShapeElementIterator)
  131.  
  132. //========================================================================================
  133. //    class CDrawContent
  134. //========================================================================================
  135.  
  136. //----------------------------------------------------------------------------------------
  137. //    CDrawContent::CDrawContent
  138. //----------------------------------------------------------------------------------------
  139. //    CDrawContent constructor
  140.  
  141. CDrawContent::CDrawContent(Environment* ev, CDrawPart* part) :
  142.     FW_CEmbeddingContent(ev, part),
  143.     fShapeList(NULL),
  144.     fDrawPart(part),
  145.     fProxyShapeCount(0)
  146. {
  147.     fShapeList = FW_NEW(CShapeCollection, ());
  148.     
  149.     FW_END_CONSTRUCTOR
  150. }
  151.  
  152. //----------------------------------------------------------------------------------------
  153. //    CDrawContent::CDrawContent
  154. //----------------------------------------------------------------------------------------
  155.  
  156. CDrawContent::CDrawContent(Environment* ev, CDrawContent* other) :
  157.     FW_CEmbeddingContent(ev, other->fDrawPart),
  158.     fShapeList(NULL),
  159.     fDrawPart(other->fDrawPart)
  160. {
  161.     fShapeList = FW_NEW(CShapeCollection, ());
  162.  
  163.     CDrawContentShapeIterator ite(other);
  164.     for (CBaseShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  165.     {
  166.         this->AddShape(ev, shape);
  167.     }
  168.  
  169.     FW_END_CONSTRUCTOR
  170. }
  171.  
  172. //----------------------------------------------------------------------------------------
  173. //    CDrawContent::~CDrawContent
  174. //----------------------------------------------------------------------------------------
  175. //    CDrawContent destructor
  176.  
  177. CDrawContent::~CDrawContent()
  178. {
  179.     FW_START_DESTRUCTOR
  180.     delete fShapeList;
  181. }
  182.  
  183. //----------------------------------------------------------------------------------------
  184. //    CDrawContent::AddShape
  185. //----------------------------------------------------------------------------------------
  186.  
  187. void CDrawContent::AddShape(Environment* ev, CBaseShape* shape, CBaseShape* nextShape)
  188. {
  189.     if (fShapeList->Contains(shape)) return;    // don't add shape twice
  190.  
  191.     if (nextShape == NULL)
  192.         fShapeList->AddLast(shape);
  193.     else
  194.         fShapeList->AddBefore(nextShape, shape);
  195.  
  196.     if (shape->GetShapeType() == kProxyShape)
  197.         fProxyShapeCount++;
  198. }
  199.  
  200. //----------------------------------------------------------------------------------------
  201. //    CDrawContent::RemoveShape
  202. //----------------------------------------------------------------------------------------
  203.  
  204. void CDrawContent::RemoveShape(Environment* ev, CBaseShape* shape)
  205. {
  206.     fShapeList->Remove(shape);
  207.     if (shape->GetShapeType() == kProxyShape)
  208.         fProxyShapeCount--;
  209. }
  210.  
  211. //----------------------------------------------------------------------------------------
  212. //    CDrawContent::Count
  213. //----------------------------------------------------------------------------------------
  214.  
  215. unsigned long CDrawContent::CountShapes() const
  216. {
  217.     return fShapeList->Count();
  218. }
  219.  
  220. //----------------------------------------------------------------------------------------
  221. //    CDrawContent::Count
  222. //----------------------------------------------------------------------------------------
  223.  
  224. void CDrawContent::EmptyShapes(Environment* ev)
  225. {
  226.     if (fShapeList)
  227.     {
  228.         fShapeList->RemoveAll();
  229.         fProxyShapeCount = 0;
  230.     }
  231. }
  232.  
  233. //----------------------------------------------------------------------------------------
  234. //    CDrawContent::GetFirstShape
  235. //----------------------------------------------------------------------------------------
  236.  
  237. CBaseShape* CDrawContent::GetFirstShape() const
  238. {
  239.     return fShapeList->First();
  240. }
  241.  
  242. //----------------------------------------------------------------------------------------
  243. //    CDrawContent::GetShapeAfter
  244. //----------------------------------------------------------------------------------------
  245. CBaseShape* CDrawContent::GetShapeAfter(CBaseShape* shape) const
  246. {
  247.     return fShapeList->After(shape);
  248. }
  249.  
  250. //----------------------------------------------------------------------------------------
  251. //    CDrawContent::IsEmpty
  252. //----------------------------------------------------------------------------------------
  253.  
  254. FW_Boolean CDrawContent::IsEmpty() const
  255. {
  256.     return (fShapeList->Count() == 0);
  257. }
  258.  
  259. //----------------------------------------------------------------------------------------
  260. //    CDrawContent::CalcUpdateShape
  261. //----------------------------------------------------------------------------------------
  262.  
  263. ODShape* CDrawContent::CalcUpdateShape(Environment* ev)
  264. {
  265.     if (this->IsEmpty())
  266.         return NULL;
  267.  
  268.     ODShape* updateShape = ::FW_NewODShape(ev);
  269.  
  270.     FW_CAcquiredODShape aqTempShape = ::FW_NewODShape(ev);
  271.     FW_Boolean first = TRUE;
  272.  
  273.     CDrawContentShapeIterator ite(this);
  274.     for (CBaseShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  275.     {
  276.         shape->GetUpdateBox(ev, aqTempShape);
  277.         if (first)
  278.             updateShape->CopyFrom(ev, aqTempShape);
  279.         else
  280.             updateShape->Union(ev, aqTempShape);
  281.                         
  282.         first = FALSE;
  283.     }
  284.  
  285.     return updateShape;
  286. }
  287.  
  288. //----------------------------------------------------------------------------------------
  289. //    CDrawContent::RedrawShapes
  290. //----------------------------------------------------------------------------------------
  291.  
  292. void CDrawContent::RedrawShapes(Environment* ev)
  293. {
  294.     // Calculate the update shape and invalidate it
  295.     FW_CAcquiredODShape updateShape = CalcUpdateShape(ev);
  296.     if (updateShape != NULL) 
  297.         this->RedrawShape(ev, updateShape);
  298. }
  299.  
  300. //----------------------------------------------------------------------------------------
  301. //    CDrawContent::RedrawShape
  302. //----------------------------------------------------------------------------------------
  303.  
  304. void CDrawContent::RedrawShape(Environment* ev, CBaseShape* shape)
  305. {
  306.     FW_CAcquiredODShape updateShape = ::FW_NewODShape(ev);
  307.     
  308.     shape->GetUpdateBox(ev, updateShape);
  309.     this->RedrawShape(ev, updateShape);
  310. }
  311.  
  312. //----------------------------------------------------------------------------------------
  313. //    CDrawContent::RedrawShape
  314. //----------------------------------------------------------------------------------------
  315.  
  316. void CDrawContent::RedrawShape(Environment* ev, ODShape* odShape)
  317. {
  318.     FW_CPresentation* presentation = fDrawPart->GetMainPresentation();
  319.     CDrawFacetClipper facetClipper(ev, fDrawPart);
  320.     facetClipper.Clip(ev, presentation, odShape);
  321.     presentation->Invalidate(ev, odShape);
  322. }
  323.  
  324. //----------------------------------------------------------------------------------------
  325. //    CDrawContent::OffsetShapes
  326. //----------------------------------------------------------------------------------------
  327.  
  328. void CDrawContent::OffsetShapes(Environment* ev, const FW_CPoint& offset)
  329. {
  330.     CShapeCollectionIterator it(fShapeList);
  331.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  332.     {
  333.         shape->OffsetShape(ev, offset.x, offset.y);
  334.     }
  335. }
  336.  
  337. //----------------------------------------------------------------------------------------
  338. //    CDrawContent::IsOKtoWrite
  339. //----------------------------------------------------------------------------------------
  340.  
  341. FW_Boolean CDrawContent::IsOKtoWrite(Environment* ev, CBaseShape* /*shape*/)
  342. {
  343.     return TRUE;    // the default
  344. }
  345.  
  346. //----------------------------------------------------------------------------------------
  347. //    CDrawContent::PostInternalizeShape
  348. //----------------------------------------------------------------------------------------
  349.  
  350. void CDrawContent::PostInternalizeShape(Environment* ev, const FW_CPoint& offset, CBaseShape* /*shape*/, short /*i*/)
  351. {
  352.     // Default is to do nothing
  353. }
  354.  
  355. //----------------------------------------------------------------------------------------
  356. //    CDrawContent::ExternalizeShapeList
  357. //----------------------------------------------------------------------------------------
  358.  
  359. void CDrawContent::ExternalizeShapeList(Environment* ev,
  360.                                         ODStorageUnit* storageUnit, 
  361.                                         FW_CCloneInfo* cloneInfo,
  362.                                         FW_Fixed offsetX,
  363.                                         FW_Fixed offsetY)
  364. {
  365.     // ----- Create an archive for our shapes -----
  366.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fDrawPart->GetPartKind(ev));
  367.     CDrawWritableStream archive(ev, suSink, suSink, cloneInfo);
  368.     
  369.     // ----- Write number of shapes -----
  370.     unsigned long count = fShapeList->Count();
  371.     archive << count;
  372.     
  373.     // ----- Write top, left offsets -----
  374.     archive << offsetX;
  375.     archive << offsetY;
  376.  
  377.     // ----- Write shapes -----
  378.     short extIndex = 1;    // externalization index, used by links to identify shapes
  379.     CShapeCollectionIterator ite(fShapeList);
  380.     for (CBaseShape* theShape = ite.First(); ite.IsNotComplete(); theShape = ite.Next())
  381.     {
  382.         if (this->IsOKtoWrite(ev, theShape))
  383.         {
  384.             FW_WRITE_DYNAMIC_OBJECT(archive, theShape, CBaseShape);
  385.             theShape->SetExternalizationIndex(extIndex++);    // so links can write their shapes' indices
  386.         }
  387.     }    
  388. }
  389.  
  390. //----------------------------------------------------------------------------------------
  391. //    CDrawContent::InternalizeShapeList
  392. //----------------------------------------------------------------------------------------
  393.  
  394. void CDrawContent::InternalizeShapeList(Environment* ev,
  395.                                         ODStorageUnit* storageUnit, 
  396.                                         FW_CCloneInfo* cloneInfo)
  397. {
  398.     // ----- Create an Archive for our shapes -----
  399.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fDrawPart->GetPartKind(ev));
  400.     FW_PBufferedSink sink(ev, suSink);
  401.     CDrawReadableStream archive(ev, fDrawPart, sink, suSink, cloneInfo);
  402.     
  403.     // ----- Read number of shapes -----
  404.     unsigned long count;
  405.     archive >> count;
  406.  
  407.     // ----- Read top left offset -----
  408.     FW_CPoint offset;
  409.     archive >> offset.x;
  410.     archive >> offset.y;
  411.  
  412.     for (short i = 1; i<=count; i++)
  413.     {
  414.         CBaseShape* theShape = NULL;
  415.         FW_READ_DYNAMIC_OBJECT(archive, &theShape, CBaseShape);
  416.         FW_ASSERT(theShape);
  417.         
  418.         // ----- Add the shape to the shape list -----
  419.         this->AddShape(ev, theShape);
  420.  
  421.         // ----- Do whatever else needs to be done to the shape -----
  422.         this->PostInternalizeShape(ev, offset, theShape, i);
  423.     }
  424. }
  425.  
  426. //----------------------------------------------------------------------------------------
  427. //    CDrawContent::IsDataOnlyOneProxy
  428. //----------------------------------------------------------------------------------------
  429.  
  430. FW_MProxy* CDrawContent::IsDataOnlyOneProxy(Environment* ev) const
  431. {
  432.     if (fProxyShapeCount == 1 && CountShapes() == 1)
  433.     {
  434.         return (CProxyShape*)GetFirstShape();
  435.     }
  436.     
  437.     return NULL;
  438. }
  439.  
  440. //----------------------------------------------------------------------------------------
  441. //    CDrawContent::AddSingleEmbeddedFrame
  442. //----------------------------------------------------------------------------------------
  443.  
  444. CProxyShape* CDrawContent::AddSingleEmbeddedFrame(Environment* ev, 
  445.                                                   FW_CEmbeddingFrame* scopeFrame,
  446.                                                   ODPart* embeddedPart, 
  447.                                                   ODFrame* embeddedFrame,
  448.                                                   ODShape* suggestedShape,
  449.                                                   ODTypeToken viewType)
  450. {
  451.     // ----- Step 2: Calculate the default shape rect
  452.     FW_CRect shapeRect;
  453.     if (suggestedShape)
  454.     {
  455.         shapeRect = FW_GetShapeBoundingBox(ev, suggestedShape);
  456.         shapeRect.Offset(-shapeRect.left, -shapeRect.top);
  457.     }
  458.     else
  459.     {
  460.         shapeRect.SetInt(0, 0, 80, 80);
  461.     }
  462.  
  463.     // ----- Step 3: Calculate the shape of the embedded frame -----    
  464.     FW_CAcquiredODShape aqFrameShape = ::FW_NewODShape(ev, shapeRect);
  465.     
  466.     // ----- Step 4: Calculate its position -----
  467.     // ----- We are placing it in the middle of the content view -----
  468.     FW_CRect frameBounds = scopeFrame->GetContentView(ev)->GetBoundsInContent(ev);
  469.     shapeRect.PlaceInCenter(frameBounds);
  470.     
  471.     // ----- Step 5: Create the proxy shape -----
  472.     CProxyShape* proxyShape = FW_NEW(CProxyShape, (ev, shapeRect, fDrawPart));
  473.     // Make sure that if an exception occurs before we complete this method that
  474.     // we dispose of the shape
  475.     
  476.     FW_TRY
  477.     {
  478.         scopeFrame->GetPresentation(ev)->Embed(ev, 
  479.                                                 embeddedPart, 
  480.                                                 embeddedFrame,
  481.                                                 kODFrameObject,        // I want persistent frames
  482.                                                 proxyShape,
  483.                                                 aqFrameShape,
  484.                                                 viewType,
  485.                                                 NULL,        // no presentation
  486.                                                 0,            // group id
  487.                                                 FALSE,        // IsOverlaid
  488.                                                 FALSE);        // sub frame
  489.     }
  490.     FW_CATCH_BEGIN
  491.     FW_CATCH_EVERYTHING () {
  492.         // cleanup for Step 5
  493.         delete proxyShape;
  494.         FW_THROW_SAME ();
  495.     }
  496.     FW_CATCH_END
  497.  
  498.     // Step 6: ----- Add the shape to our list -----
  499.     this->AddShape(ev, proxyShape);
  500.     
  501.     return proxyShape;
  502. }
  503.  
  504. //========================================================================================
  505. //    class CDrawContentShapeIterator
  506. //========================================================================================
  507.  
  508. //----------------------------------------------------------------------------------------
  509. //    CDrawContentShapeIterator::CDrawContentShapeIterator
  510. //----------------------------------------------------------------------------------------
  511.  
  512. CDrawContentShapeIterator::CDrawContentShapeIterator(CDrawContent* content) :
  513.     CShapeCollectionIterator(content->fShapeList)
  514. {
  515.     FW_END_CONSTRUCTOR
  516. }
  517.  
  518. //----------------------------------------------------------------------------------------
  519. //    CDrawContentShapeIterator::~CDrawContentShapeIterator
  520. //----------------------------------------------------------------------------------------
  521.  
  522. CDrawContentShapeIterator::~CDrawContentShapeIterator()
  523. {
  524.     FW_START_DESTRUCTOR
  525. }
  526.  
  527. //========================================================================================
  528. //    class CSemanticShapeElementIterator
  529. //========================================================================================
  530.  
  531. //----------------------------------------------------------------------------------------
  532. //    CSemanticShapeElementIterator::CSemanticShapeElementIterator
  533. //----------------------------------------------------------------------------------------
  534.  
  535. CSemanticShapeElementIterator::CSemanticShapeElementIterator(CDrawContent* content) :
  536.     fImplementation(content->fShapeList)
  537. {
  538.     FW_END_CONSTRUCTOR
  539. }
  540.  
  541. //----------------------------------------------------------------------------------------
  542. //    CSemanticShapeElementIterator::~CSemanticShapeElementIterator
  543. //----------------------------------------------------------------------------------------
  544.  
  545. CSemanticShapeElementIterator::~CSemanticShapeElementIterator()
  546. {
  547.     FW_START_DESTRUCTOR
  548. }
  549.  
  550.